﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using OA.BLL;

using OA.Model;
using OA.Model.DB;
using OA.Model.View;
using OA.Web.Areas.Member.Controllers;
using OA.Web.Attribute;
using OA.Web.Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web;


namespace OA.Web.Areas.Management.Controllers
{
    [Area("Management")]
    public class ProductController : BaseMemberController
    {

        private IMemoryCache _cache;
        public ProductController(ICookie cookie, IHttpContextAccessor accessor, IMemoryCache cache) : base(accessor, cookie, cache)
        {
            _cache = cache;
        }

        [MyActionFilter("207003", "产品管理", "单位与产品", true)]
        public IActionResult Index()
        {
            return View();
        }

        [MyActionFilter("207003", "产品管理")]
        public IActionResult GetData(string keyword, int page = 1, int limit = 20)
        {
            Expression<Func<VProduct, bool>> where = m => true;
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                where = m => (m.Name.Contains(keyword));
            }
            PageList<VProduct> list = this.GetPageList<VProduct>(where, page, limit);
            var result = new
            {
                code = "0",
                msg = "",
                count = list.RowCount,
                data = list.List
            };
            return Json(result);
        }

        [HttpGet]
        [MyActionFilter("207003001", "添加类别", "产品管理", false, "")]
        public IActionResult Create()
        {
            return View(new Product());
        }

        [HttpPost]
        [MyActionFilter("207003001", "添加类别", "产品管理", false, isUpdateDataBase: true)]
        public IActionResult Create(Product model, List<int> Department)
        {
            BaseResult baseResult = new BaseResult();
            Department = Department.Where(m => m > 0).ToList();
            if(Department.Count==0)
            {
                this.AddInfo<Product>(model);
                baseResult.Msg = "请选择一个产品类别";
                return Json(baseResult);
            }
            model.CategoryID = Department.Last();
            model.CreateDateTime = DateTime.Now;
           

             

            if (!ModelState.IsValid)
            {
                baseResult.State = 400;
                baseResult.Msg = this.GetModelStateError(ModelState);                 
                return Json(baseResult);
            }
            else
            {
                this.AddInfo<Product>(model);
                baseResult.Msg = "产品添加成功";
                return Json(baseResult);

            }

        }

        [HttpGet]
        [MyActionFilter("207003002", "编辑产品信息", "产品管理", false, "")]
        public IActionResult Edit(int id)
        {
            Product infoProduct = this.GetInfo<Product>(id);
            if (infoProduct == null)
            {
                return Redirect("/Admin/Home/ShowInfo?msg=" + System.Web.HttpUtility.UrlEncode("沒有找到该产品信息", Encoding.UTF8));
            }
            return View(infoProduct);
        }

        [HttpPost]
        [MyActionFilter("207003002", "编辑产品信息", "产品管理", false,isUpdateDataBase:true)]
        public IActionResult Edit(int id, Product model, List<int> Department)
        {
            BaseResult baseResult = new BaseResult();
            Department = Department.Where(m => m > 0).ToList();
            if (Department.Count == 0)
            {
                this.AddInfo<Product>(model);
                baseResult.Msg = "请选择一个产品类别";
                return Json(baseResult);
            }
            model.CategoryID = Department.Last();

            if (!ModelState.IsValid)
            {
                baseResult.State = 400;
                baseResult.Msg = this.GetModelStateError(ModelState);
                return Json(baseResult);
            }
            Product infoPageGroup = this.GetInfo<Product>(id);
            if (infoPageGroup == null)
            {
                baseResult.State = 400;
                baseResult.Msg = "沒有找到该产品信息";
                return Json(baseResult);
                 
            }
            this.Update<Product>(infoPageGroup, model,null, new string[] { "ID", "CreateDateTime"});
            baseResult.Msg = "产品信息修改成功";
            return Json(baseResult);
        }

        [HttpGet]
        [MyActionFilter("207003003", "删除产品", "产品管理", false, "")]
        public IActionResult Delete(long id)
        {
            Expression<Func<Product, bool>> where = m => m.ID == id;
            this.Delete<Product>(where);
            return Json(new { State = 200, Msg = "删除成功" });
        }
    }
}
